﻿<%@ Page Language="VB" AutoEventWireup="false" CodeFile="editor.aspx.vb" Inherits="editor"  ValidateRequest="false" %>
<%@ Import Namespace="System.Xml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Editor Page</title>

<script type="text/javascript" src="javascript/jquery/jq-prompt.js"></script>
<script type="text/javascript" src="editor/tiny_mce.js"></script>

<script type="text/javascript">

function saveAs() {

  fileName = document.getElementById("fileName").value;
  if(fileName) {document.getElementById("fileForm").submit();}

    else { 
  
        var askName = Prompt.show('Ievadiet Datnes nosaukumu:');
          if(askName) { 
          
            document.getElementById("fileName").value = askName 
                document.getElementById("fileForm").submit();
                
                }
            }
        } 
        

	tinyMCE.init({
	
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example word content CSS (should be your site CSS) this one removes paragraph margins
		content_css : "css/word.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
		
	function showEditor() {document.getElementById("editor").style.display="block";}
	
</script>
</head>

<body onload="showEditor()">

<%
    Dim fileName As String
    fileName = Request.QueryString("fileName")
    
    If fileName = "" Then
        fileName = Request.Form("fileName")
    End If
        
    Dim fileContent As String = Request.Form("elm1")
        
    If fileContent <> "" Then
        If fileName <> "" Then
        
            Dim fileSystem = Server.CreateObject("Scripting.FileSystemObject")
            Dim fileObject = fileSystem.CreateTextFile(Server.MapPath("\filesystem\" & fileName))
           
            fileObject.WriteLine(fileContent)
    
            
            Dim xmlDoc As New XmlDocument()
            xmlDoc.Load(Server.MapPath("xml_files.xml"))
            Dim node As XmlNode = xmlDoc.DocumentElement
            
            Dim item As XmlElement = xmlDoc.CreateElement("item")
                
            item.SetAttribute("name", fileName)
            item.SetAttribute("level", "public")
            item.SetAttribute("type", "page")
         
            'item.AppendChild(name)
    
            node.InsertBefore(item, node.ChildNodes(0))
            xmlDoc.Save(Server.MapPath("xml_files.xml"))
    
            
            
            'Response.Write("File created: " & f.DateCreated)
            
            
    
            fileObject = Nothing
            fileSystem = Nothing
        
        End If
    End If
        
    %>

<div style="position:absolute; top:-2px; left:-2px; width:635px; height:300px; overflow:hidden; display:none;" id="editor">
<form method="post" action="editor.aspx" id="fileForm" onsubmit="saveAs(); return false;">

	<textarea id="elm1" name="elm1" rows="15" cols="70" style="height:370px;">
		<%=fileContent%>
	</textarea>

<input type="hidden" name="fileName" id="fileName" value="<%=fileName%>" />
	
<!--
 
    <input type="submit" name="save" value="Submit" />
	<input type="reset" name="reset" value="Reset" /> 
	
	-->
	
</form>    
    
</div>    
</body>
</html>
